04. Exercise: Getting Started

Exercise: Getting Started

The best way to improve your coding skills is to practice and experiment with the code for yourself. So let's get started with some hands-on exercises! We'll start with some simple ones that walk you through the process step by step, and gradually build up your skills until, by the end of the course, you have everything you need to build a complete Java app.

For the first few exercises in this course, we'll be demonstrating them in workspaces (like the one below) right here in the Udacity classroom. These simulate the same simple setup you would get if you wrote your Java code in a primitive code editor and ran it using a BASH Shell Command-Line Interface (CLI).

If you like, you can also (optionally) try these out in the IntelliJ IDEA, which you should have installed earlier. We won't be providing explicit instructions for using IntelliJ until later in the course (when we will be using IntelliJ exclusively), but you're certainly welcome to give it a try if you're feeling adventurous.

Printing Output

For this first exercise, our goal is simply to create a short program (or script) that prints out a message. To print output in Java, we can use System.out.println. For example:

System.out.println("Hello world!!");

This takes whatever message we place in the parentheses and prints that message. In this case, it will print `"Hello world!" in the CLI.

Compiling and Running the Code

When you want to test your program, you cannot simply run it—you must first compile the code. You can do this by using the javac command, followed by the name of the file you want to compile. In the below example, our file is called GettingStarted.java. So in this case, we will compile the code by running:

javac GettingStarted.java

Once the code is compiled, we can run it. To do this, we simply enter java followed by the name of the file. In this exercise it will be:

java GettingStarted.java

Boilerplate Code

In the workspace below, you'll find some boilerplate starter code that looks like this:

public class GettingStarted{

    public static void main(String[] args) {

    }

}

We need this code in order to get the program to work, but you don't need to understand it at this stage—it will make more sense once we get into classes and objects in a later lesson. For now, the important thing to notice is the main method:

    public static void main(String[] args) {

    }

The main method is the entry point for the program. When we say it is the"entry point", we mean that everything will start running from here. So for this exercise, we will want to add all our code inside the main method (between the opening and closing braces { }).

One last thing to notice about the above code is the indentation. You'll notice that some of the code is indented by four spaces. This indentation is used to help show which blocks of code are nested inside of other blocks. The standard practice in Java is typically to indent code by four spaces.

Hello world!

Task Description:

Let's try out a simple "Hello world!" program in Java.

If you have any trouble with the code, you can find a complete copy of the solution at the bottom of this page.

Task List:

Task Feedback:

Nice work!

Code

If you need a code on the https://github.com/udacity.

  • userCode:

    export PATH=/data/jdk-15.0.1/bin:$PATH
    export JAVA_HOME=/data/jdk-15.0.1/bin

  • Code Comments

    You'll notice that the starter code included a comment:

    // Add your code here:

    In Java, you can add two slashes // to create a single-line code comment. All of the text to the right of the // is treated as a comment and ignored (i.e., it does not get treated as code and Java will not try to run it when the program executes).

    You can also create multi-line code comments by using /* and */. For example:

    /* This is a multi-line code comment.
    All of this text in here will be treated as
    a comment rather than code! */

    A Note About Exercise Solutions

    You can find a solution for the exercise below. Whenever you do an exercise, we'll always provide the solution afterward. A couple things to note:

    • It's totally OK and normal to get stuck sometimes. You should not feel bad when you need to check the answer.
    • That said, you should always give the exercise a good try yourself first before looking at the solution. Even if you do get stuck, you will learn better if you give it a try first.
    • There is almost always more than one way to solve a problem when programming—so your code may look different from ours. That's OK! Try to compare your code with the solution to see how it is the same and how it is different.

    Solution

    Here's our code for this exercise:

    public class GettingStarted{
    
        public static void main(String[] args) {
            // Add your code here:
            System.out.println("Hello world!");
        }
    
    }